#include "stdafx.h"
#include "Label.h"

using namespace ZeEngine;
using namespace std;

Label::Label(const string& font, const std::string& strText, const Vector2D<s32>& position, u32 fontSize, SDL_Color color) : 
                                                                                                                                GUIObject(),
                                                                                                                                m_text(strText),
                                                                                                                                m_fontSize(fontSize),
                                                                                                                                m_textChanged(true),
                                                                                                                                m_color(color),
                                                                                                                                m_pTexture(NULL)

{
    m_pFont = LoadResource<Font>("game_interface", font);
    Init(position, Vector2D<float>());
}

Label::~Label()
{
}

void Label::SetText(const std::string& strText)
{
    if (m_text != strText)
    {
        m_text = strText;
        m_textChanged = true;
    }
}

void Label::SetColor(SDL_Color color)
{
    if (m_color.a != color.a || 
        m_color.b != color.b || 
        m_color.g != color.g || 
        m_color.r != color.r)
    {
        m_color = color;
        m_textChanged = true;
    }
}

void Label::SetFontSize(u32 size)
{
    if (m_fontSize != size)
    {
        m_fontSize = size;
        m_textChanged = true;
    }
}

void Label::SetTexture(SDL_Texture* pTexture)
{
    Utilities::SafeDelete<SDLTexture>(m_pTexture);

    if (pTexture)
    {
        m_pTexture = new SDLTexture(pTexture);

        if (m_pTexture)
        {
            Init(GetRelativePosition(), m_pTexture->GetDimension());
            m_textChanged = false;
        }
    }
}

void Label::Render(SDL_Renderer& renderer)
{          
    SDLRenderer& sdlRenderer = GetRenderer();

    if (m_textChanged)
    {
        if (m_pFont)
        {
            SDL_Texture* pTexture = sdlRenderer.CreateRenderedText(renderer, m_pFont, m_text, m_color, m_fontSize);

            if (pTexture)
            {
                SetTexture(pTexture);
            }
        }
    }

    if (m_pTexture)
    {
        Vector2D<float> position = GetAbsolutePosition();  

        SDL_Texture* pTexture = m_pTexture->GetTexture();
        Vector2D<s32> size = m_pTexture->GetDimension();

        SDL_Rect src = {0, 0, size.GetX(), size.GetY()};
        SDL_Rect dest = { static_cast<int>(position.GetX()), static_cast<int>(position.GetY()), 
                         static_cast<int>(size.GetX()), static_cast<int>(size.GetY()) };

        if (pTexture)
        {
            SDL_RenderCopyEx(&renderer, pTexture, &src, &dest, 0, NULL, SDL_FLIP_NONE);
        }        
    }
}